package com.yksj.healthtalk.ui.chatting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.LevelListDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 
 * 邀请好友
 * @author zhao
 */
public class InviteAttentionActivity extends BaseFragmentActivity implements  OnClickListener {
	int mType;//查询类型
	String groupId;
	InviteFriendListener mFriendListener;
	
	/**
	 * 好友邀请监听
	 * @author zhao
	 */
	public interface InviteFriendListener{
		List<String> onInvite();//邀请的id
	}
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.invite_list_friends_layout);
		mType = getIntent().getIntExtra("type",0);
		groupId = getIntent().getStringExtra("groupId");
		
		initView();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2://邀请
			onSubmitInviteFriends();
			break;
		}
	}
	
	private void initView(){
		initTitle();
		
		setBackBtn("返回", this);
		
		setRight("确定", this);
		
		setTitle("邀请加入");
		
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		Fragment fragment = null;
		if(mType == 1){//邀请关注的人
			titleTextV.setText("已关注的人");
			fragment = Fragment.instantiate(this,InviteAtationedFragment.class.getName());
		}else if(mType == 2){//多美号邀请
			titleTextV.setText("按昵称或多美号邀请");
			fragment = Fragment.instantiate(this,InviteByNameFragment.class.getName());
		}else if(mType == 3){//条件
			CustomerInfoEntity entity = getIntent().getParcelableExtra("mCustomerInfoEntity");
			fragment = Fragment.instantiate(this,InviteConditionFragment.class.getName());
			Bundle args = new Bundle();
			args.putParcelable("mCustomerInfoEntity", entity);
			fragment.setArguments(args);
		}else if(mType == 4){//附近的人
			titleTextV.setText("身边的人");
			fragment = Fragment.instantiate(this,InviteByNearbyFragment.class.getName());
		}
		
		mFriendListener = (InviteFriendListener)fragment;
		ft.add(R.id.fragmentcontent,fragment);
		ft.commitAllowingStateLoss();
	}
	
	/**
	 * 提交邀请
	 */
	private void onSubmitInviteFriends(){
		if(mFriendListener != null){
			List<String> list = mFriendListener.onInvite();
			if(list != null && list.size()>0){
				SmartFoxClient.sendGroupInvite(this,groupId,list);
				ToastUtil.showShort(getApplicationContext(),"邀请已发出");
			}
		}
	}
	
	public static class FriendsListAdapter extends BaseAdapter{
		final Map<String, String> mChoosedMap = new HashMap<String, String>();
		final List<CustomerInfoEntity> entities = new ArrayList<CustomerInfoEntity>();
		LayoutInflater mInflater;
		private final ImageLoader mImageLoader;
		private final Resources mResources;
		
		public FriendsListAdapter(LayoutInflater inflater,Context context){
			this.mInflater = inflater;
			this.mResources = context.getResources();
			this.mImageLoader = ImageLoader.getInstance();
		}
		
		public void onDataChange(List<CustomerInfoEntity> list){
			entities.addAll(list);
			notifyDataSetChanged();
		}
		
		public void onChoose(String id,boolean isChoosed){
			if(isChoosed){
				mChoosedMap.put(id,id);
			}else{
				mChoosedMap.remove(id);
			}
		}
		
		@Override
		public int getCount() {
			return entities.size();
		}
		
		@Override
		public boolean isEmpty() {
			return false;
		}

		@Override
		public CustomerInfoEntity getItem(int position) {
			return entities.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder viewHolder;
			final CustomerInfoEntity entity = getItem(position);
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.friendlist_item1,null);
				viewHolder.headerImageV = (ImageView)convertView.findViewById(R.id.head_image);
				viewHolder.nameTextV = (TextView)convertView.findViewById(R.id.name);
				viewHolder.sexImageV = (ImageView)convertView.findViewById(R.id.head_sex);
				viewHolder.levelImageV = (ImageView)convertView.findViewById(R.id.levl);
				viewHolder.attention = (ImageView)convertView.findViewById(R.id.attention);
				viewHolder.noteTextV = (TextView)convertView.findViewById(R.id.note);
				viewHolder.chooseBox = (CheckBox)convertView.findViewById(R.id.choose);
				viewHolder.chooseBox.setVisibility(View.VISIBLE);
				viewHolder.chooseBox.setButtonDrawable(R.drawable.check_xunze);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
			}
			
			viewHolder.attention.setVisibility(View.GONE);
			LevelListDrawable listDrawable = (LevelListDrawable)viewHolder.sexImageV.getDrawable();
			listDrawable.setLevel(Integer.valueOf(entity.getSex()));
			viewHolder.nameTextV.setText(entity.getName());
			viewHolder.noteTextV.setText(entity.getDescription());
			viewHolder.levelImageV.setVisibility(entity.isShowDoctorV() ? View.VISIBLE:View.GONE);
			viewHolder.chooseBox.setChecked(mChoosedMap.containsKey(entity.getId()));
			viewHolder.chooseBox.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					CheckBox box  = (CheckBox)v;
					if(box.isChecked()){
						mChoosedMap.put(entity.getId(), entity.getId());
					}else{
						mChoosedMap.remove(entity.getId());
					}
				}
			});
			mImageLoader.displayImage(entity.getSex(), entity.getNormalHeadIcon(),viewHolder.headerImageV);
			return convertView;
		}
	}
	
	public static class ViewHolder{
		ImageView headerImageV;
		TextView nameTextV;
		ImageView sexImageV;
		ImageView levelImageV;
		ImageView attention;
		TextView noteTextV;
		CheckBox chooseBox;
	}
	
}
